package logic

import (
	"230311house/models/cache"
	"230311house/models/mysql"
	"context"
	"fmt"

	"230311house/hourpc/hourpc"
	"230311house/hourpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type LonginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewLonginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LonginLogic {
	return &LonginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *LonginLogic) Longin(in *hourpc.LonginRequest) (*hourpc.LonginResponse, error) {
	// todo: add your logic here and delete this line
	get := cache.RDB.Get(l.ctx, "sendSms"+in.Account)
	if get.Val() != in.SendCode {
		return nil, fmt.Errorf("短信获取失败")
	}
	var user mysql.User
	mysql.DB.Debug().Where("account =?", in.Account).Find(&user)
	if user.ID == 0 {
		return nil, fmt.Errorf("当前用户不存在,请前去注册")
	}
	if user.Password != in.Password {
		return nil, fmt.Errorf("密码错误")
	}
	return &hourpc.LonginResponse{
		Id: int64(user.ID),
	}, nil
}
